package com.engr.shiro;

import com.engr.enums.SearchStatusEnum;
import com.engr.model.Permission;
import com.engr.service.PermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: train-shiro
 * @desc: shiro 服务层接口
 * @author: lyx
 * @date: 2018-11-11 14:54
 **/
@Service
public class ShiroService {

    @Autowired
    private PermissionService permissionService;

    /**
     * 初始化权限
     */
    public Map<String,String> loadFitlerChainDefinitions(){
        // 权限控制map 静态资源以及登录页面权限
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/register", "anon");
        filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/error/**", "anon");
        filterChainDefinitionMap.put("/kickout", "anon");
        /*filterChainDefinitionMap.put("/logout", "logout");*/
        filterChainDefinitionMap.put("/css/**","anon");
        filterChainDefinitionMap.put("/js/**","anon");
        filterChainDefinitionMap.put("/img/**","anon");
        filterChainDefinitionMap.put("/libs/**","anon");
        filterChainDefinitionMap.put("/favicon.ico", "anon");
        filterChainDefinitionMap.put("/verificationCode", "anon");
        // 从数据库获取权限
        List<Permission> permissions = permissionService.selectAllByStatus(SearchStatusEnum.EFFECTIVE.getCode());
        permissions.forEach(x->{
            if (StringUtils.isNotBlank(x.getUrl()) && StringUtils.isNotBlank(x.getPerms())) {
                String perm = "perms[" + x.getPerms() + "]";
                filterChainDefinitionMap.put(x.getUrl(),perm + ",kickout");
            }
        });
        filterChainDefinitionMap.put("/**", "user,kickout");

        return filterChainDefinitionMap;
    }

}
